<?php

/**
 * This is the model class for table "offers".
 *
 * The followings are the available columns in table 'offers':
 * @property integer $id
 * @property string $title
 * @property string $image
 * @property string $start
 * @property string $finish
 * @property string $sell_start
 * @property string $sell_finish
 * @property integer $price
 * @property integer $discount
 * @property string $terms
 * @property string $description
 * @property string $description_marked
 */
class Offer extends CActiveRecord
{
	var $preid = 'offers';
	public 
	$images = array(
		'image' => array(
			'field' => 'image',
			'filename' => '%id%',
			'alt' => '%title%',
			'title' => '%title%', 
			'watermark' => array('big'),
			'bg' => '#000000',
			'file_type' => 'jpg',
			'sizes' => array(
				'normal'=>array(120, 70),
				'main'=>array(415, 240),
				'big'=>array(430, 300),
			),
		),
	);
	/**
	 * Returns the static model of the specified AR class.
	 * @return Offer the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'offers';
	}
	
	/**
	 * Returns a list of behaviors that this model should behave as.
	 * @return array of behavior configurations indexed by behavior names
	 */
	public function behaviors(){
		return array(
			'BCImage' => array(
				'class' => 'BCImage',
				'conf' => $this->images,
			),
		);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, image, price, discount, description', 'required'),
			array('price, discount', 'numerical', 'integerOnly'=>true),
			array('title, image', 'length', 'max'=>255),
			array('start, finish, sell_start, sell_finish, terms', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, image, start, terms, finish, sell_start, sell_finish, price, discount, terms, description, description_marked', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	public function scopes(){
		$db = $this->getDbConnection();
		$alias = $db->quoteColumnName($this->getTableAlias());
		
		return array(
			'coming' => array(
				'condition' => $alias.'.sell_start > now()',
			),
			'active' => array(
				'condition' => $alias.'.sell_start <= now() and '.$alias.'.sell_finish >= now()',
			),
			'archived' => array(
				'condition' => $alias.'.sell_finish < now()',
			),
		);
	}
	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Название',
			'image' => 'Картинка',
			'start' => 'Начало акции',
			'finish' => 'Окончание акции',
			'sell_start' => 'Начало публикации купонов',
			'sell_finish' => 'Окончание публикации купонов',
			'price' => 'Цена услуги',
			'discount' => 'Размер скидки',
			'terms' => 'Условия акции (через точку с запятой)',
			'description' => 'Описание',
			'description_marked' => 'Описание',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('start',$this->start,true);
		$criteria->compare('finish',$this->finish,true);
		$criteria->compare('sell_start',$this->sell_start,true);
		$criteria->compare('sell_finish',$this->sell_finish,true);
		$criteria->compare('price',$this->price);
		$criteria->compare('discount',$this->discount);
		$criteria->compare('terms',$this->terms,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('description_marked',$this->description_marked,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function beforeValidate(){
		Yii::import('ext.markdown.*');
		$md = new EMarkdown;
		
		$this->description_marked = $md->transform($this->description);

		return parent::beforeValidate();
	}
	
}